<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登录日志</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div id="water-wrapper"></div>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item" style="margin-left: -20px">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <label>
                                    <input type="text" name="username" autocomplete="off" class="layui-input"
                                           placeholder="输入用户名搜索">
                                </label>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">ip</label>
                            <div class="layui-input-inline">
                                <label>
                                    <input type="text" name="ip" autocomplete="off" class="layui-input"
                                           placeholder="输入ip搜索">
                                </label>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <!-- 查询按钮-->
                            <button type="submit" class="layui-btn " lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon layui-icon-search"
                                                                    style="color: white"></i>
                            </button>
                            <!--重置按钮-->
                            <button type="reset" class="layui-btn " lay-submit
                                    lay-filter="data-reset-btn"><i class="layui-icon layui-icon-refresh"
                                                                   style="color: white"></i>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        //查询后渲染页面
        function render(url) {
            table.render({
                    elem: '#currentTableId',
                    url: url,
                    headers: {
                        'Authorization': localStorage.getItem('token')
                    },
                    toolbar: '#toolbarDemo',
                    page: true,
                    limit: 10,
                    limits: [10],
                    height: 600,
                    even: true,
                    size: 'md',
                    totalRow: true,
                    curr: 1,
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    parseData: function (res) {
                        return {
                            "code": res.status === 200 ? 0 : 500, //解析接口状态
                            "msg": res.message, //解析提示文本
                            "count": res.page.count, //解析数据长度
                            "data": res.page.results,
                            "curr": res.page.pageNum
                        }
                    },
                    cols: [[
                        // {type: "checkbox"},
                        {field: 'id', title: 'ID', sort: true, align: "center"},
                        {field: 'username', title: '用户名', align: "center"},
                        {field: 'ip', title: 'ip', align: "center"},
                        {
                            field: 'login_time', align: "center", title: '登录时间', sort: true, templet: function (d) {
                                return layui.util.toDateString(d.login_time, 'yyyy-MM-dd HH:mm:ss')
                            }
                        }

                    ]]
                }
            );
        }
        // 表格渲染
        render('http://127.0.0.1:8000/user/user/login/logs/')
        //重置刷新页面
        form.on('submit(data-reset-btn)', function (data) {
            // 执行重置操作
            window.location.reload();
        })

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var uname = data.field.username;
            var ip = data.field.ip;
            if (uname === '' && ip === '') {
                layer.msg('请输入用户名或ip')
            } else if (ip === '') {
                $.ajax({
                    url: 'http://127.0.0.1:8000/user/user/login/cat/' + uname + '/',

                    type: 'get',
                    headers: {
                        'Authorization': localStorage.getItem('token')
                    },
                    dataType: 'json',
                    success: function (res) {
                        //渲染表格

                        render('http://127.0.0.1:8000/user/user/login/cat/' + uname + '/')
                    }
                })
            } else if (uname === '') {
                $.ajax({
                    url: 'http://127.0.0.1:8000/user/user/login/cat/' + ip + '/',

                    type: 'get',
                    headers: {
                        'Authorization': localStorage.getItem('token')
                    },
                    dataType: 'json',
                    success: function (res) {
                        //渲染表格

                        render('http://127.0.0.1:8000/user/user/login/cat/' + ip + '/')
                    }
                })
            }

            return false;
        });


    });

</script>
<script src="../js/custom/water.js" charset="utf-8"></script>
<script src="../js/yanhua.js" charset="utf-8"></script>
</body>
</html>